import React, { PureComponent } from 'react';
import { View, Text, TextInput } from 'react-native';
import { Provider } from 'react-redux';

import { configureStore } from './store';
import {
  ErrorInfo,
  Loading,
  OverlayContainer,
  LoginFailureDialog,
} from './component';
import Router from './router';
import { AppStateUtils } from './utils';

//字体不随系统字体变化

Text.defaultProps = Object.assign({}, Text.defaultProps, {
  allowFontScaling: false,
});

export default class Root extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      storeReady: false,
    };

    this.store = configureStore(() => {
      this.setState({
        storeReady: true,
      });
    });
  }

  componentWillMount() {
    AppStateUtils.addChangeEventListener();
  }

  componentWillUnmount() {
    AppStateUtils.removeChangeEventListener();
  }

  render() {
    if (!this.state.storeReady) {
      return null;
    }
    return (
      <Provider store={this.store}>
        <View style={{ flex: 1 }}>
          <Router launchOptions={this.props} />
          <OverlayContainer />
          <Loading />
          <ErrorInfo />
          <LoginFailureDialog />
        </View>
      </Provider>
    );
  }
}
